//
//  MangeClass.m
//  学生管理系统
//
//  Created by mir_yan on 2018/9/30.
//  Copyright © 2018年 hakeem. All rights reserved.
//

#import "MangeClass.h"
#import "Student.h"

@interface MangeClass ()
// 数据源
@property (nonatomic) NSMutableArray* dataSource;

@end

@implementation MangeClass

// 懒加载 当使用的时候再去开辟内存
// 重写get方法
- (NSMutableArray*) dataSource {
    if (!_dataSource) {
        _dataSource = [[NSMutableArray alloc] init];
    }
    return _dataSource;
}

- (void)addStudent:(Student*)stu {
    Student * s1 = [self searchStudentWithNumber:stu.no];
    if (s1) {
        NSLog(@"学号已经存在");
    } else {
        [self.dataSource addObject:stu];
    }
}
- (void)removeStudent:(NSInteger)number {
    Student * s1 = [self searchStudentWithNumber:number];
    if (s1) {
        [self.dataSource removeObject: s1];
        NSLog(@"删除成功");
    } else {
        NSLog(@"学号找不到， 删除失败");
    }
}
- (void)removeStudentWithName:(NSString*)name {
//    isEqualToString 判断内容相等
    for (int i = 0; i < self.dataSource.count; i++) {
        Student * stu = self.dataSource[i];
        if ([name isEqualToString:stu.name]) {
            [self.dataSource removeObject:stu];
        }
    }
    // 快速枚举
//    for (Student * stu in self.dataSource) {
//        if ([name isEqualToString:stu.name]) {
//            [self.dataSource removeObject:stu];
//        }
//    }
}
- (void)updateStudent:(NSInteger)nubmer {
    Student * s1 = [self searchStudentWithNumber:nubmer];
    if (s1) {
        NSLog(@"请输入名字");
        char name[20];
        scanf("%s", name);
        
        NSLog(@"请输入成绩");
        int score;
        scanf("%d",&score);
        
        NSLog(@"请输入班级");
        char dclass[20];
        scanf("%s", dclass);
        
        // 改名字 成绩 班级
        s1.name = [[NSString alloc] initWithUTF8String:name];
        s1.score = score;
        s1.dclass = [[NSString alloc] initWithUTF8String:dclass];
        
    } else {
        NSLog(@"学号不存在，无法修改");
    }
}
- (Student*)searchStudentWithNumber:(NSInteger)number {
    for (int i = 0; i < self.dataSource.count; i++) {
        Student* stu = self.dataSource[i];
        if (stu.no == number) {
            return stu;
        }
    }
    return nil;
}
- (void)showAllStudent {
    NSLog(@"%@", self.dataSource);
}

@end
